package com.kygeng.datasearch.util;

import com.kygeng.datasearch.model.DoubleData;
import org.springframework.util.StringUtils;

/**
 * @Title: CompanyDataUtil
 * @Author: kygeng
 * @Package: com.kygeng.datamonitior.util
 * @Date: 2024/9/1 21:22
 * @Description: 转化公司数据的Util
 */
public class CompanyDataUtil {

    /**
     * 字符串格式转化：[100,200] -> 100,200
     * TODO：优化校验规则，不符合条件的直接返回 Null
     *
     * @param dataformat
     * @return
     */
    public static DoubleData getDoubleData(String dataformat) {

        if (StringUtils.isEmpty(dataformat)) {
            return null;
        }

        String string = dataformat.substring(1, dataformat.length() - 1);
        String[] split = string.split(",");

        DoubleData doubleData = new DoubleData();

        if (split.length > 0 && !StringUtils.isEmpty(split[0])) {
            doubleData.setLeft(Double.parseDouble(split[0]));
            doubleData.setLeftMark(dataformat.substring(0, 1));
        }
        if (split.length > 1 && !StringUtils.isEmpty(split[1])) {
            doubleData.setRight(Double.parseDouble(split[1]));
            doubleData.setRightMark(dataformat.substring(dataformat.length() - 1, dataformat.length()));
        }
        return doubleData;
    }

    public static void main(String[] args) {
        System.out.println(getDoubleData("[100,200]"));
        System.out.println(getDoubleData("[100,]"));
        System.out.println(getDoubleData("[,200]"));
        System.out.println(getDoubleData("[,]"));
        System.out.println(getDoubleData(",100]"));
        System.out.println(getDoubleData(",]"));
        System.out.println(getDoubleData("[200,"));
        System.out.println(getDoubleData("[,"));
    }

}
